﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace IFRS.DataAccess.DAL
{
    public class IFRS_Utility
    {

        string sConn = ConfigurationManager.ConnectionStrings["IFRS_WEB"].ConnectionString;

        public DataTable SqlDataTable(string sSql, params SqlParameter[] Parameters)
        {
            DataTable dt = new DataTable();
            SqlConnection Conn = null;
            try
            {
                Conn = new SqlConnection(sConn);
                Conn.Open();
                SqlCommand Cmd = new SqlCommand(sSql, Conn);
                Cmd.CommandType = CommandType.Text;
                if (Parameters != null)
                {
                    foreach (SqlParameter param in Parameters)
                    {
                        Cmd.Parameters.Add(param);
                    }
                }
                SqlDataReader dr = Cmd.ExecuteReader();
                dt.Load(dr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return dt;
        }

        public DataTable StoredProcDataTable(string ProcedureName, params SqlParameter[] Parameters)
        {
            DataTable dt = new DataTable();
            SqlConnection Conn = null;
            try
            {
                Conn = new SqlConnection(sConn);
                Conn.Open();
                SqlCommand Cmd = new SqlCommand(ProcedureName, Conn);
                Cmd.CommandType = CommandType.StoredProcedure;
                if (Parameters != null)
                {
                    foreach (SqlParameter param in Parameters)
                    {
                        Cmd.Parameters.Add(param);
                    }
                }
                SqlDataReader dr = Cmd.ExecuteReader();
                dt.Load(dr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return dt;
        }

        public DataTable StoredProcDataTable(string ConnectionName, string ProcedureName, params SqlParameter[] Parameters)
        {
            DataTable dt = new DataTable();
            SqlConnection Conn = null;
            try
            {
                Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
                Conn.Open();
                SqlCommand Cmd = new SqlCommand(ProcedureName, Conn);
                Cmd.CommandType = CommandType.StoredProcedure;
                if (Parameters != null)
                {
                    foreach (SqlParameter param in Parameters)
                    {
                        Cmd.Parameters.Add(param);
                    }
                }
                SqlDataReader dr = Cmd.ExecuteReader();
                dt.Load(dr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return dt;
        }

        public object ExecuteStoredProc(string ProcedureName, params SqlParameter[] Parameters)
        {
            SqlConnection Conn = null;
            SqlTransaction trn = null;
            int iNum = 0;
            try
            {
                Conn = new SqlConnection(sConn);
                Conn.Open();
                trn = Conn.BeginTransaction();
                SqlCommand Cmd = new SqlCommand(ProcedureName, Conn);
                Cmd.CommandType = CommandType.StoredProcedure;
                if (Parameters != null)
                {
                    foreach (SqlParameter param in Parameters)
                    {
                        Cmd.Parameters.Add(param);
                    }
                }
                Cmd.Transaction = trn;
                iNum = Cmd.ExecuteNonQuery();
                trn.Commit();
            }
            catch (Exception ex)
            {
                trn.Rollback();
                throw ex;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return iNum;
        }

        public object ExecuteStoredProc(string ConnectionName, string ProcedureName, params SqlParameter[] Parameters)
        {
            SqlConnection Conn = null;
            SqlTransaction trn = null;
            int iNum = 0;
            try
            {
                Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
                Conn.Open();
                trn = Conn.BeginTransaction();
                SqlCommand Cmd = new SqlCommand(ProcedureName, Conn);
                Cmd.CommandType = CommandType.StoredProcedure;
                if (Parameters != null)
                {
                    foreach (SqlParameter param in Parameters)
                    {
                        Cmd.Parameters.Add(param);
                    }
                }
                Cmd.Transaction = trn;
                iNum = Cmd.ExecuteNonQuery();
                trn.Commit();
            }
            catch (Exception ex)
            {
                trn.Rollback();
                throw ex;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return iNum;
        }

    }

    public static class ValueTypeExt
    {
        public static string NoEmpty(this string s)
        {
            return String.IsNullOrWhiteSpace(s) ? null : s.Trim();
        }

        public static object AsDBValue(this string s)
        {
            return String.IsNullOrWhiteSpace(s) ? DBNull.Value : (object)s.Trim();
        }

        public static object AsDBValue<T>(this T value)
        {
            return value == null ? DBNull.Value : (object)value;
        }

        public static string AsString(this SqlParameter param)
        {
            return param.Value == DBNull.Value ? null : (string)param.Value;
        }

        public static string AsStringNull(this SqlParameter param)
        {
            return param.Value == DBNull.Value ? "" : (string)param.Value;
        }
        public static decimal? AsDecimal(this SqlParameter param)
        {
            return param.Value == DBNull.Value ? (decimal?)null : (decimal)param.Value;
        }

        public static DateTime? AsDateTime(this SqlParameter param)
        {
            return param.Value == DBNull.Value ? (DateTime?)null : (DateTime)param.Value;
        }
        public static int AsInt32(this string s)
        {
            return s == "" ? 0 : Convert.ToInt32(s);
        }
        public static decimal? AsDecimal(this string value)
        {
            decimal d;
            return String.IsNullOrWhiteSpace(value) ? (decimal?)null : Decimal.TryParse(value, out d) ? d : (decimal?)null;
        }

        public static DateTime? AsDate(this string value)
        {
            return String.IsNullOrWhiteSpace(value) ? (DateTime?)null : DateTime.ParseExact(value, "dd/MM/yyyy", null);
        }

        public static DateTime? AsDateTime(this string value)
        {
            return String.IsNullOrWhiteSpace(value) ? (DateTime?)null : DateTime.ParseExact(value, "dd/MM/yyyy HH:mm:ss", null);
        }
    }


}
